﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;

namespace RentalSystem.Data.EF.Core
{
    public class EFManager<Type>
    {
        private RentalDatabaseEntities database;

        public RentalDatabaseEntities Database
        {
            get
            {
                return database;
            }
        }

        public EFManager(RentalDatabaseEntities repository)
        {
            this.database = repository;
        }
        
        public void Save()
        {
            database.SaveChanges();
        }

        public ObjectQuery<DbDataRecord> SQLQuery(string query)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            TranslateQuery(ref query, null);
            return database.CreateQuery<DbDataRecord>(query);
        }

        public ObjectQuery<DbDataRecord> SQLQuery(string query, QueryParam[] parameters)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            TranslateQuery(ref query, parameters);
            return database.CreateQuery<DbDataRecord>(query);
        }

        public ObjectQuery<Type> SQLQueryToEntity(string query)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            TranslateQuery(ref query, null);
            return database.CreateQuery<Type>(query);
        }

        public ObjectQuery<Type> SQLQueryToEntity(string query, QueryParam[] parameters)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            TranslateQuery(ref query, parameters);
            return database.CreateQuery<Type>(query);
        }

        public ObjectQuery<DbDataRecord> SQLQueryToObject(string query, QueryParam[] parameters)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            TranslateQuery(ref query, parameters);
            return database.CreateQuery<DbDataRecord>(query);
        }

        private void TranslateQuery(ref string query, QueryParam[] parameters)
        {
            if (query == null)
                throw new ArgumentNullException("Query string is NULL");

            query = query.Replace("FROM ", String.Format("FROM {0}.", database.GetType().Name));

            if (parameters == null)
                return;

            foreach (QueryParam param in parameters)
            {
                if (param.Key != null && param.Value != null)
                {
                    if(param.Type.Name == "String")
                        query = query.Replace(param.Key, "'"+param.Value.ToString()+"'");
                    else
                        query = query.Replace(param.Key, param.Value.ToString());
                }
            }
        }
    }
}
